<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      v-show="showSearch"
      @submit.native.prevent
    >
      <el-row :gutter="10">
        <el-col :span="4">
          <el-form-item
            prop="title"
          >
            <el-input
              v-model="queryParams.title"
              placeholder="请输入订阅数据标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row
      :gutter="10"
      class="mb8"
    >

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="subscribeList"
      @selection-change="handleSelectionChange"
      v-table
      height="0"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <!-- <el-table-column
        label="主键"
        align="center"
        prop="id"
      /> -->
      <el-table-column
        label="订阅数据标题"
        align="center"
        prop="title"
      />
      <el-table-column
        label="订阅时间"
        align="center"
        prop="subscribeTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订阅状态"
        align="center"
        prop="isSubscribe"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.isSubscribe == '0'">已订阅</span>
          <span v-else>取消订阅</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="状态0隐藏1显示"
        align="center"
        prop="status"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['compre:subscribe:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['compre:subscribe:remove']"
          >删除</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-bell"
            v-if="scope.row.isSubscribe  == '0'"
            @click="operate(scope.row)"
          >取消订阅
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-message-solid"
            v-if="scope.row.isSubscribe  == '1'"
            @click="operate(scope.row)"
          >订阅
          </el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-document-copy"
            v-clipboard:copy="copyUrl"
            v-clipboard:success="onCopy"
            v-if="scope.row.isSubscribe  == '0'"
            @click="copy(scope.row)"
          >复制url
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据订阅对话框 -->
      <!-- 添加或修改数据注册对话框 -->
      <el-dialog
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="标题"
              prop="title"
            >
              <el-input
                v-model="form.title"
                placeholder="请输入标题"
                maxlength="20"
                readonly
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">

            <el-form-item
              label="数据内容"
              prop="content"
            >
              <el-input
              readonly
                v-model="form.content"
                type="textarea"
                placeholder="请输入数据内容"
                rows="3"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="备注"
              prop="remark"
            >
              <!-- <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
                rows="3"
              /> -->
              <editor-preview
              readonly
                v-model="form.remark"
                :min-height="192"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >

        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listSubscribe,
  getSubscribe,
  delSubscribe,
  operate,
  updateSubscribe,
} from "@/api/compre/subscribe";
import { getRegister } from "@/api/compre/register";
export default {
  name: "Subscribe",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据订阅表格数据
      subscribeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      copyUrl: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据订阅列表 */
    getList() {
      this.loading = true;
      listSubscribe(this.queryParams).then((response) => {
        this.subscribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registerId: null,
        subscribeTime: null,
        isSubscribe: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据订阅";
    },
    /** 修改按钮操作 */
    /** 修改按钮操作 */
    handleView(row) {
      this.reset();
      const registerId = row.registerId || this.ids;
      getRegister(registerId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看数据订阅";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSubscribe(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubscribe(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除数据订阅编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSubscribe(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "compre/subscribe/export",
        {
          ...this.queryParams,
        },
        `subscribe_${new Date().getTime()}.xlsx`
      );
    },
    /** 删除按钮操作 */
    operate(row) {
      let message = "";
      let isSubscribe = null;
      console.log(row, "row");
      if (row.isSubscribe == "1") {
        message = "确定订阅该数据吗?";
        isSubscribe = 0;
      } else {
        message = "确定取消订阅该数据吗?";
        isSubscribe = 1;
      }

      this.$confirm(message, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return operate({ id: row.id, isSubscribe: isSubscribe });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功!");
        })
        .catch(() => {});
    },
    copy(row) {
      const url = window.location.host;
      let that = this;
      getRegister(row.registerId).then((res) => {
        // this.form = res.data.content;
        if (res.data.content) {
          that.copyUrl =
            url + "/dev-api/compre/register/getContent?id=" + row.registerId;
        } else {
          this.$message.error("源数据不存在");
        }
      });
    },
    onCopy: function (e) {
      this.$message.success("复制成功");
    },
  },
};
</script>
